import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/video/video_play_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/course_detail_detail_page/course_detail/course_detail_detail_page/evaluate_detail/evaluate_detail_view.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/course_detail_detail_page/course_detail/course_detail_detail_page/train_directory_detail/train_directory_detail_view.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/course_detail_detail_page/course_detail_detail_page_controller.dart';

/// 课程详情详情
class CourseDetailDetailPagePage extends StatelessWidget {
  CourseDetailDetailPagePage({super.key});

  final controller = Get.put(CourseDetailDetailPageController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '课程详情',
            ),
            body: DefaultTabController(
              length: 2, // This is the number of tabs.
              initialIndex: controller.initPage,
              child: PrimaryScrollController(
                controller: controller.myScrollController,
                child: NestedScrollView(
                  headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                    return <Widget>[
                      SliverList(
                        delegate: SliverChildBuilderDelegate(
                          (BuildContext context, int index) {
                            return _headInfo();
                          },
                          childCount: 1,
                        ),
                      ),
                      //吸收高度
                      SliverOverlapAbsorber(
                        handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                        sliver: SliverPersistentHeader(
                          pinned: true,
                          delegate: StickyHeader(
                            Container(
                              color: Colors.white,
                              child: TabBar(
                                enableFeedback: false,
                                tabs: const [
                                  Tab(text: '目录'),
                                  Tab(text: '问答'),
                                ],
                                dividerColor: const Color(0xFFECEEF3),
                                labelPadding: EdgeInsets.only(bottom: 10.w),
                                labelStyle: TextStyle(
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w400,
                                ),
                                unselectedLabelStyle: TextStyle(
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w400,
                                ),
                                labelColor: const Color(0xFF5C6DFF),
                                unselectedLabelColor: const Color(0xFF262F38),
                                indicatorPadding: EdgeInsets.only(top: 45.w, bottom: 0.w, right: 10, left: 10),
                                indicator: const BoxDecoration(
                                  gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [
                                    Color(0xFF5C6DFF),
                                    Color(0xFF5C6DFF),
                                  ]),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ];
                  },
                  body: Container(
                    padding: EdgeInsets.only(top: 45.w),
                    color: const Color(0xffF5F5F6),
                    child: TabBarView(
                      children: [
                        TrainDirectoryDetailPage(),
                        EvaluateDetailPage(),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        });
  }

  Widget _headInfo() {
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          controller.textbookDetailEntity?.textbookForm != null && controller.directoryFile.isNotEmpty
              ? controller.textbookDetailEntity?.textbookForm == 0 //	课程形式 0:录播 1：仅课件
                  ? VideoPlayWidget(
                      key: controller.videoKey,
                      // url: Interface.fileUrl+controller.directoryFile,
                      url: controller.directoryFile,
                      autoPlay: true,
                      isDispose: true,
                    )
                  : Container(
                      height: 175.w,
                      decoration: BoxDecoration(
                          image: DecorationImage(
                        image: NetworkImage('${Interface.fileUrl}${controller.textbookDetailEntity?.textbookCover}'),
                      )),
                    )
              : Container(),
          Padding(
            padding: EdgeInsets.only(top: 12.w, left: 16.w,),
            child: Text(
              controller.textbookDetailEntity?.textbookName ?? '',
              style: TextStyle(
                color: const Color(0xFF262F38),
                fontSize: 16.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          )
        ],
      ),
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  StickyTabBarDelegate({required this.child});

  final TabBar child;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  double get maxExtent => child.preferredSize.height;

  @override
  double get minExtent => child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}

class StickyHeader extends SliverPersistentHeaderDelegate {
  StickyHeader(
    this.child,
  );

  final Widget child;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  double get maxExtent => 50.0;

  @override
  double get minExtent => 50.0;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}

